<template>
	<view id="login">	
		<Header txt="登录" v-if="baidu"></Header>
		<HeaderB txt="登录" v-if="!baidu"></HeaderB>
		<view class="top-tit">
			你好，
			<view class="tit">欢迎来到熊猫代理</view>
		</view>
		<view>		
			<input v-model="loginForm.account" placeholder="请输入手机号" autocomplete="off" />
		</view>
		<view>		
			<input type="password" password=true v-model="loginForm.password"  placeholder="请输入密码"  />
		</view>
		<label>
			<text class="pass" @click="gofo">忘记密码?</text>
		</label>
		<view  id="captcha1">	
		</view>
		<view class="">
			<button @click="changeLogin" class="loginbtn" v-if="isWx==true">立刻登录</button>
			<button @click="changeLogin" class="loginbtn" v-if="isWx==false">账号登录</button>
			<!-- <button @click="appLoginWx" class="loginbtn loginbtn2" v-if="isWx==false">微信授权登录</button> -->
			<button @click="gopage" class="registerbtn">去注册</button>
		</view>	
	</view>
</template>

<script>
	import {formatDate2} from '../../common/com.js'
	export default {
		data() {
			return {
				loginForm: {
				        account: "",
				        password: "",
				        remember: true,
				    },
				dom: "",
				password: "",
				obj:{},
				isWx:true,
				code:0,
				baidu:true
			};
		},
		onShow() {
			// #ifdef MP-WEIXIN
				this.isWx=false
			// #endif
			// #ifdef MP-BAIDU
				this.baidu=false
			// #endif
		},
		onLoad(){
			let that = this;
			// #ifdef MP-WEIXIN
				 uni.getSetting({
				    success(res) {
						console.log("授权：",res);
						if (!res.authSetting['scope.userInfo']) {
							//这里调用授权
							console.log("当前未授权");
						} else {
							//用户已经授权过了
							console.log("当前已授权");
						}
				    }
				})
			// #endif
			
		},
		computed:{
			go(){
				return this.$store.state.isLogin
			},
			firstLogin() {
			      return this.$store.state.firstLogin;
			    },
		},
		methods: {
			
			// 微信授权
			appLoginWx(e){
				uni.navigateTo({
					url:"/pages/login/loginwx"
				})
			},				
			//登录
			getSetting(code,state) {
				let _params
				  _params = {
				    code: code,
				    state: state,
				    source: 'APP',
				  };  
				this.$store
				  .dispatch("CallBack",_params)
				  .then((response) => {
					console.log(response);
				  })
				  .catch(() => {}); 
			},			
			gofo(){
				uni.navigateTo({
					url:"/pages/login/forgetPas"
				})
			},
			gopage(){
				uni.navigateTo({
					url:"/pages/login/register"
				})
			},
			getUserInfo() {
				let that = this;
				this.$store
				  .dispatch("GetInfo")
				  .then((response) => {
				    // that.cert.status = response.data.obj.status;
				  })
				  .catch(() => {});      
			},
			changeLogin() {
				let that=this
				let _params
				  _params = {
				    account: this.loginForm.account,
				    password: this.loginForm.password,
				    originType: 1,
				  };        
				this.$store
				  .dispatch("Login",
					_params
				  ).then((response)=>{
					  if(response.data.code==0){
						  this.getUserInfo()
					  	// #ifdef H5
					  	alert("欢迎回来，尊敬的客户")
					  	let obj = {
					  	    account: this.loginForm.account,
					  	    password:this.loginForm.password,
					  	};
					  	uni.setStorageSync("userLogin", JSON.stringify(obj));					
					  	uni.switchTab({
					  		url: "/pages/user/user"
					  	})
					  	// #endif
					  	// #ifndef H5
						this.getUserInfo()
					  		uni.showModal({
					  		    title: '登陆成功',
					  		    content: '欢迎回来，尊敬的客户',
					  		    success: function (res) {									
					  		        if (res.confirm) {
					  				   let obj = {
					  				       account: that.loginForm.account,    
					  				       password:that.loginForm.password,       
					  				   };
									   that.$store.commit("login",obj)
									   uni.setStorageSync('userLogin', obj);
									   // #ifdef MP-WEIXIN									   
									   uni.setStorageSync('Cookie', response.header['Set-Cookie']);
									   uni.login({
									   	success: (res) => {
									   		// 客户端代码
											console.log(res);
									   		uniCloud.callFunction({
									   		    name: 'user-wx',
									   			data:{
									   				code:res.code
									   			},
									   		    success(res){
									   				let data={
									   					id:res.result.openid
									   				}
									   				uni.setStorageSync('wxId',data)			 
									   		    },
									   		    fail(res){
									   				console.log("实拍",res);
									   				uni.showToast({
									   					title:"失败"+res
									   				})
									   		    }
									   		})						
									   	}
									   })
									   // #endif
									   // #ifdef MP-BAIDU
									   uni.setStorageSync('Cookie', response.header['set-cookie']);
									   if(response.header['set-cookie']==''||response.header['set-cookie']==null||response.header['set-cookie']==undefined){
									   	uni.setStorageSync('Cookie', response.header['Set-Cookie']);
									   }										
									   // #endif 
										// #ifdef MP-ALIPAY
										uni.login({
											success: (res) => {
												// 客户端代码
												uniCloud.callFunction({
												    name: 'user-alipay',
													data:{
														code:res.code
													},
												    success(res){
														let data={
															id:res.result.openid
														}
														uni.setStorageSync('alipayId',data)					 
												    },
												    fail(res){
														console.log("实拍",res);
														uni.showToast({
															title:"失败"+res
														})
												    }
												})						
											}
										})
										// #endif 
					  		           uni.switchTab({
					  		           	url: "/pages/user/user"
					  		           })
					  		        } else if (res.cancel) {
					  		            console.log('用户点击取消');
					  		        }
					  		    }
					  		});
						// #endif
					  }else{
						  uni.showModal({
							 title: response.data.msg
						  })
						  that.$store.commit("SET_VAL", false);
					  }
				  })
			},
		}
	}
</script>

<style lang="less" >
	#login{
		width: 90%;
		margin: 0 auto;
		.pass{
			margin-left: 30rpx;			
			font-size: 30rpx;
			color: #9A9A9A;
			cursor: pointer;
		}
		.loginbtn{
			margin: 0 auto;
			background-color: rgb(72, 110, 245);
			color: rgb(255, 255, 255);
			border-radius: 100rpx;
			margin-top: 80rpx;
		}
		.loginbtn2{
			width: 300rpx;
			height: 66rpx;
			line-height: 66rpx;
			background-color: rgb(255, 255, 255);
			border: 2rpx solid #cccccc;
			color: rgb(153, 153, 153);
			margin-top: 30rpx;
		}
		.registerbtn{
			margin: 0 auto;
			background-color: rgb(255, 255, 255);
			color: rgb(153, 153, 153);
			border: 2rpx solid #cccccc;
			margin-top: 70rpx;
			width: 150rpx;
			font-size: 24rpx;
			border-radius: 100rpx;
		}
		.top-tit{
			font-size: 60rpx;
			color: #222222;
			font-weight: bold;
			margin-top: 80rpx;
			.tit{
				font-size: 36rpx;
				padding-top: 20rpx;
				margin-bottom: 100rpx;
			}    
		}
		input {
			width: 100%;
			height: 80rpx;
			margin-bottom: 10rpx;
			border-radius: 80rpx;
			background-color: #f4f4f4;
			color: #525252;
			padding-left: 20rpx;
			margin-bottom: 50rpx;
		}
		.span1 {
		      // float: left;
		      width: 14px;
			  height: 14px;
		      cursor: pointer;
		      color: #3373fc;
		      &:hover {
		        opacity: 0.8;
		      }
			  
		 }
		uni-checkbox .uni-checkbox-input {
			width: 14px;
			height: 14px;
		}
		button{
			margin-top: 30rpx;
			margin-bottom: 30rpx;
		}
		button::after{
			border:none;
		}
	}

</style>
